package dao

import (
	"fmt"
	"hft/Http/Models/request"
	utils "hft/Http/Utils"

	"gorm.io/gorm"
)

type Clue struct{}

func (c *Clue) Name(name string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {

	return func(db *gorm.DB) *gorm.DB {

		var newName = utils.DeleteStringSpace(name)

		if newName == "" {
			return db
		}

		return db.Where("page_name LIKE ?", fmt.Sprintf("%v%%", name))
	}
}

func (c *Clue) Phone(phone string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if phone == "" {
			return db
		}
		return db.Where("phone = ?", phone)
	}
}

func (c *Clue) Activity(id int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if id == 0 {
			return db
		}
		return db.Where("activity_id = ?", id)
	}
}

func (c *Clue) OriginName(name string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if name == "" {
			return db
		}
		return db.Where("origin_name = ?", name)
	}
}

func (c *Clue) Region(link string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if link == "" {
			return db
		}
		return db.Where("region_link LIKE ?", link)
	}
}

func (c *Clue) Level(level string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if level == "" {
			return db
		}
		return db.Where("level = ?", level)
	}
}

func (c *Clue) CreateTime(time []string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if len(time) != 2 {
			return db
		}
		return db.Where("create_time > ? AND create_time < ?", time[0], time[1])
	}
}

func (c *Clue) UpdateTime(time []string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if len(time) != 2 {
			return db
		}
		return db.Where("update_time > ? AND update_time < ?", time[0], time[1])
	}
}

func (c *Clue) BrokerTime(time []string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if len(time) != 2 {
			return db
		}

		return db.Where("update_time > ? AND update_time < ?", time[0], time[1])
	}
}

func (c *Clue) Valid(status int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if status == 1 || status == 2 {
			return db.Where("valid = ?", status)
		}

		return db
	}
}

func (c *Clue) IsShield(shield bool, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("is_shield = ?", shield)
	}
}

func (c *Clue) Preload(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Preload("ClueList", func(db *gorm.DB) *gorm.DB {
			return db.Omit("ip_region,cont").Order("create_time desc")
		})
	}
}

func (c *Clue) Account(accountLink string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("account_link LIKE ?", accountLink)
	}
}

func (c *Clue) SearchUser(paginate *request.SearchCluePage, accountLink string, regionLink string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Select("*").Omit("region_link").Scopes(c.Preload(db)).Joins("User").Group("`User`.`id`").Scopes(c.Name(paginate.LouPanName, db)).Scopes(c.Phone(paginate.Phone, db)).Scopes(c.Activity(paginate.ActivityId, db)).Scopes(c.OriginName(paginate.OriginName, db)).Scopes(c.Region(regionLink, db)).Scopes(c.Level(paginate.Level, db)).Scopes(c.CreateTime(paginate.CreateTime, db)).Scopes(c.UpdateTime(paginate.UpdateTime, db)).Scopes(c.BrokerTime(paginate.BrokerTime, db)).Scopes(c.Valid(paginate.Valid, db)).Scopes(c.IsShield(paginate.IsShield, db)).Scopes(c.Account(accountLink, db)).Order("User.update_time desc")
	}

}

var ClueDao = new(Clue)
